<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery/jquery.min.js"></script>

</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            用户信息修改
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputEmail1" class="col-sm-2 control-label">用户名称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="username" class="form-control username" id="username" value="李思思">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail2" class="col-sm-2 control-label">昵称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="nickname" class="form-control nickname" id="nickname" value="哈哈思">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">邮箱：</label>
                    <div class="col-sm-4">
                        <input type="email" name="email" class="form-control email" id="email" value="520@163.com">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">用户图标：</label>
                    <div class="col-sm-10">
                        <img src="images/2.jpg" alt="" class="user_pic" id="userPic">
                        <input style="display: block" name="userPic" type="file" id="exampleInputFile"
                            onchange="changeUrl()">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputEmail4" class="col-sm-2 control-label">密码：</label>
                    <div class="col-sm-4">
                        <input type="password" name="password" class="form-control password" id="password"
                            value="12345678">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success btn-edit">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="./js/http.js"></script>
    <script>
        $(function () {
            // 获取用户详情
            getdetail_user_info();
        })
        // 获取用户信息
        function getdetail_user_info() {
            let xml = new XMLHttpRequest();
            xml.open('GET', 'http://localhost:8080/api/v1/admin/user/detail');
            xml.setRequestHeader('Authorization', localStorage.getItem('login_toke'))
            xml.send();
            xml.onload = function () {
                let res = JSON.parse(xml.responseText);
                console.log(res);
                if (res.code !== 200) return;
                let data = res.data;
                $('#username').val(data.username);
                $('#nickname').val(data.nickname);
                $('#email').val(data.email);
                $('#userPic').attr('src', data.userPic);
                $('#password').val(data.password);
            }
        }
        // 修改
        $('#form').submit(function (e) {
            e.preventDefault();
            // let xhr = new XMLHttpRequest();
            // xhr.open('POST', 'http://localhost:8080/api/v1/admin/user/edit');
            // // formData数据格式，原生ajax不需要设置那个请求头
            // xhr.setRequestHeader('Authorization', localStorage.getItem('login_toke'));
            // // formData格式提交
            let file = document.getElementById('exampleInputFile').files[0];
            let formData = new FormData();
            let paramsObj = {
                username: $('#username').val(),
                nickname: $('#nickname').val(),
                email: $('#email').val(),
                userPic: file,
                password: $('#password').val()
            }
            for (let key in paramsObj) {
                formData.append(key, paramsObj[key]);
            }
            // formData.append('username', $('#username').val());
            // formData.append('nickname', $('#nickname').val());
            // formData.append('email', $('#email').val());
            // formData.append('userPic', file);
            // formData.append('password', $('#password').val());
            // xhr.send(formData);
            // xhr.onload = function () {
            //     let res = JSON.parse(xhr.responseText);
            //     console.log(res);
            //     if (res.code !== 200) return;
            // }
            // jq方式
            myAjax('POST', '/admin/user/edit', formData, function (resData) {
                console.log(resData);
                if (resData.code !== 200) return;
                // iframe调用父级window对象下的方法
                window.parent.get_user_info();
            })
        })
        // input图片改变本地显示
        function changeUrl() {
            let file = document.getElementById('exampleInputFile').files[0];
            let local_url = URL.createObjectURL(file);
            $('#userPic').attr('src', local_url);
        }
    </script>
</body>

</html>